package edu.kit.ksri.db.mturk.plugins.correctness.answers;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Logger;

import edu.kit.ksri.db.mturk.var.StaticInformation;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 * 
 * ---------------------------------------------------------------------------
 * 
 * Purpose: this factory class offers createXYZInstance methods for all subclasses of
 * {@link AbstractAssignmentAnswer} which require more than the raw answer
 * {@link String} as input parameter
 * 
 * Also more generalized methods are provided for future extensions
 */
public class AssignmentAnswerFactory {

	private static Logger basiclog = Logger.getLogger("basic." + AssignmentAnswerFactory.class);

	/**
	 * creates a new instance of {@link BasicAssignmentAnswer}. Is more
	 * efficient than createInstance (~0ms vs. ~15ms per instance).
	 * 
	 * @param rawAnswer
	 * @return
	 */
	public static BasicAssignmentAnswer createBasicAssignmentAnswerInstance(
			String rawAnswer) {
		return new BasicAssignmentAnswer(rawAnswer,
				StaticInformation.scenario.getBasicAssignmentAnswerMode());
	}

	
	/**
	 * creates a new instance of {@link ICDCompositeAssignmentAnswer}.
	 * 
	 * @param rawAnswer
	 * @return
	 */
	public static ICDCompositeAssignmentAnswer createICDCompositeAssignmentAnswerInstance(
			String rawAnswer) {
		return new ICDCompositeAssignmentAnswer(rawAnswer, StaticInformation.scenario.getBasicAssignmentAnswerMode());
		//if necessary use additional parameters next to rawAnswer
	}
	
	
	/**
	 * tries to create an object based on {@link StaticInformation}
	 * .answerComparisonClass using also the parameter list defined in
	 * {@link StaticInformation}.answerComparisonClassParameters. Will either
	 * return an instance of that class or null but not throw Exceptions
	 * 
	 * @param rawAnswer
	 * @return
	 */
	public static AbstractAssignmentAnswer createInstance(String rawAnswer) {
		try {
			Class<?>[] paramTypes = new Class[StaticInformation.scenario.getAnswerComparisonClassParameters().length + 1];
			Object[] params = new Object[StaticInformation.scenario.getAnswerComparisonClassParameters().length + 1];
			paramTypes[0] = String.class;
			params[0] = rawAnswer;
			int counter = 1;
			for (Object o : StaticInformation.scenario.getAnswerComparisonClassParameters()) {
				paramTypes[counter] = o.getClass();
				params[counter++] = o;
			}

			Constructor<?> c = StaticInformation.scenario.getAnswerComparisonClass()
					.getConstructor(paramTypes);
			return (AbstractAssignmentAnswer) c.newInstance(params);
		} catch (SecurityException e) {
			basiclog.error(e);
		} catch (NoSuchMethodException e) {
			basiclog.error(e);
		} catch (IllegalArgumentException e) {
			basiclog.error(e);
		} catch (InstantiationException e) {
			basiclog.error(e);
		} catch (IllegalAccessException e) {
			basiclog.error(e);
		} catch (InvocationTargetException e) {
			basiclog.error(e);
		}
		return null;
	}

}
